package com.musicapp.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.musicapp.R;
import com.musicapp.home.HomeActivity;
import com.musicapp.register.RegisterActivity;

public class LoginActivity extends AppCompatActivity {

    private EditText passwordEditText;
    private boolean isPasswordVisible = false;
    private EditText usernameEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        passwordEditText = findViewById(R.id.password);
        Button registerButton = findViewById(R.id.registerButton); // 确保这里的ID与布局文件中的ID一致
        Button loginButton = findViewById(R.id.loginButton); // 确保这里的ID与布局文件中的ID一致
        usernameEditText = findViewById(R.id.username);

        passwordEditText.setOnTouchListener((v, motionEvent) -> {
            final int DRAWABLE_RIGHT = 2;
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                if (motionEvent.getRawX() >= (passwordEditText.getRight() - passwordEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    isPasswordVisible = !isPasswordVisible;
                    if (isPasswordVisible) {
                        passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        passwordEditText.setSelection(passwordEditText.length()); // Move cursor to the end of text
                        passwordEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_hide_password, 0);
                    } else {
                        passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        passwordEditText.setSelection(passwordEditText.length()); // Move cursor to the end of text
                        passwordEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_show_password, 0);
                    }
                }
            }
            return false;
        });

        registerButton.setOnClickListener(v -> {
            // Handle registration logic here
            startActivity(new Intent(this, RegisterActivity.class));
            finish();
        });

        loginButton.setOnClickListener(v -> {
            // Handle login logic here
            if (validateLogin(usernameEditText.getText().toString(), passwordEditText.getText().toString())) {
                // 保存登录状态，例如使用SharedPreferences
                // 这里假设登录成功，跳转到HomeActivity
                startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                finish(); // 结束当前Activity
            } else {
                // 显示错误消息
                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }

        });
    }

    private boolean validateLogin(String username, String password) {
        // 在这里添加你的登录验证逻辑，比如从服务器验证用户名和密码
        // 这里只是一个简单的示例，假设用户名为"admin"且密码为"123456"时登录成功
        return "admin".equals(username) && "123456".equals(password);
    }
}
